layui.use(['table', 'admin', 'ax', 'form', 'layer', 'jquery','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var layer = layui.layer;
    var laydate = layui.laydate;

    var proid = Feng.getUrlParam('id');

    /**
     * 检索区域
     */
        var qyAjax = new $ax("/area/getAreaByPid");
        qyAjax.set("pid", 216);//山西省
        var qyResult = qyAjax.start();
        $("#cityId").html("");
        $("#cityId").append("<option value=''>全部</option>");
        $.each(qyResult, function (index, data) {
            $("#cityId").append("<option value='" + data.id + "'>" + data.simpleName + "</option>");
        });
        form.render('select');

    //YYJL
    var ajax1 = new $ax(Feng.ctxPath + "/project/selectYyjlList", function (data) {
        var sb;
        for(let i = 0; i < data.length; i++) {
            sb+='<option value="'+data[i].tel+'">' + data[i].yyjl + '</option>';
        }
        $("#yyjl").append(sb);
        form.render('select');
    }, function (data) {
        Feng.error(data.responseJSON.message);
    });
    ajax1.start();

    //DD
    form.on('select(yyjl)', function(data){
        $('#dd option').not('option:first').remove();
        if(data.value!=''){
            var ajax1 = new $ax(Feng.ctxPath + "/project/selectDdList", function (data) {
                var sb;
                for(let i = 0; i < data.length; i++) {
                    $("#dd").append('<option value="'+data[i].tel+'">' + data[i].dd + '</option>');
                }
                // alert(sb)
                // $("#dd").append(sb);
                form.render();
            }, function (data) {
                Feng.error(data.responseJSON.message);
            });
            ajax1.set("phone",data.value);
            ajax1.start();
        }
    });



    laydate.render({
        elem: '#openDate'
        ,format: 'yyyy-MM-dd'
    });

    if(proid!='' && proid!=null ){

        var ajax = new $ax(Feng.ctxPath + "/project/detail", function (data) {

            if(data.success){
                var v = data.data;
                if(v.openDate!=''){
                    v.openDate=v.openDate.substring(0,10);
                }
                form.val('gcForm',v);

                if(v.provinceId!=''){
                    var qyAjax = new $ax("/area/getAreaByPid");
                    qyAjax.set("pid", v.provinceId);
                    var qyResult = qyAjax.start();
                    $("#cityId").html("");
                    $("#cityId").append("<option value=''>全部</option>");
                    $.each(qyResult, function (index, data) {
                        $("#cityId").append("<option value='" + data.id + "' "+(v.cityId==data.id?'selected':'')+">" + data.simpleName + "</option>");
                    });
                    form.render('select');
                }

                if(v.yyjl!=''){
                    var qyAjax = new $ax("/project/selectDdList");
                    qyAjax.set("phone", v.yyjl);
                    var qyResult = qyAjax.start();
                    $("#dd").html("");
                    $("#dd").append("<option value=''>全部</option>");

                    $.each(qyResult, function (index, data) {
                        $("#dd").append("<option value='" + data.tel + "' "+(v.yyjl==data.tel?'selected':'')+">" + data.dd + "</option>");
                    });
                    form.render('select');
                }

                if(v.dd!=''){
                    var qyAjax = new $ax("/project/selectDdList");
                    qyAjax.set("phone", v.yyjl);
                    var qyResult = qyAjax.start();
                    $("#dd").html("");
                    $("#dd").append("<option value=''>全部</option>");

                    $.each(qyResult, function (index, data) {
                        $("#dd").append("<option value='" + data.tel + "' "+(v.dd==data.tel?'selected':'')+">" + data.dd + "</option>");
                    });
                    form.render('select');
                }

            }
            form.render('select');
        }, function (data) {
            Feng.error(data.responseJSON.message);
        });
        ajax.set('id',proid);
        ajax.start();
    }

    /**
     * 关店按钮点击事件
     */
    var pageindex = parent.layer.getFrameIndex(window.name);
    $('#backupPage').click(function () {
        parent.layer.close(pageindex);
    });
    /**
     * 表单提交事件
     */
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/project/addFinishSx", function (data) {
            if(data.success){
                Feng.success("添加成功！");
                //传给上个页面，刷新table用
                admin.putTempData('formOk', true);
                //关掉对话框
                admin.closeThisDialog();
            }else{
                Feng.error(data.message);
            }
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message);
        });
        ajax.set(data.field);
        ajax.set("yyName",$("#yyjl").find("option:selected").text());
        ajax.start();
    });




});
